<?php
namespace app\crm\controller;
use think\Loader;

class Goods extends CrmController
{
    /**
     * 商品列表界面
     * @return [type] [description]
     */
    public function goodsList()
    {	
		//分配增删改的权限
		$menuname = "Goods List";
		$p_menuname = $_SESSION['menu'][$menuname]; //父菜单
		$priv = $_SESSION["user_priv"][$p_menuname][$menuname];		
		$this->assign("username",$_SESSION['user_info']['username']);
        $this->assign("priv",$priv);
		
		$typeData = Loader::model('Goods')->getShopType();
        $this->assign("typeData",$typeData);
		$cateData = Loader::model('Goods')->getShopCategory();
        $this->assign("cateData",$cateData);
		$brandData = Loader::model('Goods')->getShopBrand();
        $this->assign("brandData",$brandData);
        // 模板输出
        return $this->fetch('goodsList');
    }

	/**
	 * 添加商品界面
	 */
    public function addGoods()
    {
        $typeData = Loader::model('Goods')->getShopType();
        $this->assign("typeData",$typeData);
		$cateData = Loader::model('Goods')->getShopCategory();
        $this->assign("cateData",$cateData);
		$brandData = Loader::model('Goods')->getShopBrand();
        $this->assign("brandData",$brandData);
		
		// 模板输出
        return $this->fetch('addGoods');
    }
	
	/**
     * 调用model的商品insertGoods方法
     */
    public function insertGoods()
    {
		$type_id = $_REQUEST['type_id'];
		$good_attribute = Loader::model('Goods')->addAttribute($type_id);
		
		$arrData = array(
			"good_type"=>$type_id,
			"good_attribute"=>$good_attribute,
			"good_name"=>$_REQUEST['good_name'],
			"good_norm"=>$_REQUEST['good_norm'],
			"good_num"=>"G".time(),   //商品货号
			"good_Inventory"=>$_REQUEST['good_Inventory'],
			"cate_id"=>$_REQUEST['cate_id'],
			"brand_id"=>$_REQUEST['brand_id'],
			"price"=>$_REQUEST['price'],
			"good_description"=>$_REQUEST['good_description'],
			"simple_description"=>$_REQUEST['simple_description'],
			"good_enabled"=>$_REQUEST['good_enabled'],
			"createtime"=>date("Y-m-d H:i:s"),
		);
		
        Loader::model('Goods')->insertGoods($arrData,$_REQUEST['good_name']);
    }
	
	/**
	 * 更新商品界面
	 */
    public function editGoods()
    {
        $id = $_GET["id"];
		$typeData = Loader::model('Goods')->getShopType();
        $this->assign("typeData",$typeData);
		$cateData = Loader::model('Goods')->getShopCategory();
        $this->assign("cateData",$cateData);
		$brandData = Loader::model('Goods')->getShopBrand();
		$this->assign("brandData",$brandData);
		
		$arrData = Loader::model('Goods')->getGoodsArr($id);
        $this->assign("arrData",$arrData);
        // 模板输出
        return $this->fetch('editGoods');
    }
	
	/**
     * 调用model的商品updateGoods方法
     */
    public function updateGoods()
    {
        $id = $_GET["id"];
		
		$type_id = $_REQUEST['type_id'];
		$good_attribute = Loader::model('Goods')->addAttribute($type_id);
		
		$data = array(
			"good_type"=>$type_id,
			"good_attribute"=>$good_attribute,
			"good_name"=>$_REQUEST['good_name'],
			"good_norm"=>$_REQUEST['good_norm'],
			"good_Inventory"=>$_REQUEST['good_Inventory'],
			"cate_id"=>$_REQUEST['cate_id'],
			"brand_id"=>$_REQUEST['brand_id'],
			"price"=>$_REQUEST['price'],
			"good_description"=>$_REQUEST['good_description'],
			"simple_description"=>$_REQUEST['simple_description'],
			"good_enabled"=>$_REQUEST['good_enabled'],
			"createtime"=>date("Y-m-d H:i:s"),
		);
        Loader::model('Goods')->updateGoods($id,$data);
    }


    /**
     * 删除商品
     * @return [type] [description]
     */
    public function delGoods()
    {
        $id = $_REQUEST["id"];
        Loader::model('Goods')->delGoods($id);
    }
	
	/**
     * 获取商品列表数据
     */
    public function goodsDataList(){
        $page = $_REQUEST["page"];
        $limit = $_REQUEST["limit"];
		//需先判断传值是否为空
        $where = "(1 ";
        $where .= empty($_REQUEST["good_name"])?"":" AND g.good_name like '%".$_REQUEST["good_name"]."%'";
        $where .= empty($_REQUEST["good_num"])?"":" AND g.good_num = '".$_REQUEST["good_num"]."'";
        $where .= empty($_REQUEST["cat_name"])?"":" AND g.cate_id = '".$_REQUEST["cat_name"]."'";
		$where .= empty($_REQUEST["brand_name"])?"":" AND g.brand_id = '".$_REQUEST["brand_name"]."'";
		$where .= empty($_REQUEST["caty_name"])?"":" AND g.good_type = '".$_REQUEST["caty_name"]."'";
        $where .= " )";
		
        $arrData = Loader::model('Goods')->getGoodsData($page,$limit,$where);
        echo json_encode($arrData);
    }
	
	/**
     * 获取商品属性值
     */
	public function attributeValues(){
		$id = $_REQUEST['id'];
		$arrData = Loader::model('Goods')->attributeValues($id);
		echo json_encode($arrData);
	}

	



}